package com.zimmur.app.mocolink.background.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zimmur.app.mocolink.background.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.background.common.StatusCodeEnum;
import com.zimmur.app.mocolink.domain.model.ShoppingCartExample;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.ShoppingCartVo;
import com.zimmur.app.mocolink.service.IShoppingCartService;

@RestController
@RequestMapping("/restful/cart")
public class ShoppingCartController extends BaseController {

	@Autowired
	private IShoppingCartService shoppingCartService;
	
	@RequestMapping("/deleteGoods")
	public ReturnJsonEntity deleteGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String goodsIds=getParam("goodsId");
		String cartIds=getParam("cartIds");
		Integer userId = getTokenUserId();
		String[] goodsIdArray = null;
		String[] cartIdArray = null;
		if(!StringUtils.isBlank(goodsIds)){
			goodsIdArray = goodsIds.split(",");
		}
		if(!StringUtils.isBlank(cartIds)){
			cartIdArray = cartIds.split(",");
		}
		int flag = shoppingCartService.deleteCartByUserId(cartIdArray, goodsIdArray, userId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/addGoods")
	public ReturnJsonEntity addGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String items=getParam("items");
		Integer goodsId = getParam("goodsId", 0);
		Integer userId = getTokenUserId();
		
		if(goodsId>0){
			int flag = shoppingCartService.checkByGoodsId(goodsId, userId);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE500401;
			}else{
				if(!StringUtils.isBlank(items)){
					String[] cartInfos=items.split(";");
					flag = shoppingCartService.addCart(cartInfos, userId);
					if(flag>0){
						codeEnum = StatusCodeEnum.CODE000000;
					}
				}
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/update")
	public ReturnJsonEntity update(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String cartInfos=getParam("cartInfo");
		if(!StringUtils.isBlank(cartInfos)){
			String[] cartInfoArray = cartInfos.split(";");
			int flag = shoppingCartService.updateCart(cartInfoArray);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/addGroupGoods")
	public ReturnJsonEntity addGroupGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer groupGoodsId = getParam("groupGoodsId", 0);
		
		Integer userId = getTokenUserId();
		if(groupGoodsId>0){
			int flag =shoppingCartService.addGroupGoods(groupGoodsId, userId);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}else{
				codeEnum = StatusCodeEnum.CODE500403;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/goodsList")
	public ReturnJsonEntity goodsList(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		List<ShoppingCartVo> cartList=shoppingCartService.selectUserCart(userId);
		if(cartList!=null){
			codeEnum = StatusCodeEnum.CODE000000;
			entity.setData(cartList);
		}
		entity.init(codeEnum);
		return entity;
	}
}
